module libeventd.buffer.Event;

import
	libeventd.Types;

enum {
	BEV_EVENT_READING	=0x01 ,
	BEV_EVENT_WRITING	=0x02 ,
	BEV_EVENT_EOF		=0x10 ,
	BEV_EVENT_ERROR		=0x20 ,
	BEV_EVENT_TIMEOUT	=0x40 ,
	BEV_EVENT_CONNECTED	=0x80 ,
}

enum bufferevent_options {
	BEV_OPT_CLOSE_ON_FREE = (1<<0),
	BEV_OPT_THREADSAFE = (1<<1),
	BEV_OPT_DEFER_CALLBACKS = (1<<2),
	BEV_OPT_UNLOCK_CALLBACKS = (1<<3)
}


enum bufferevent_filter_result {
	BEV_OK = 0,
	BEV_NEED_MORE = 1,
	BEV_ERROR = 2
}

enum bufferevent_flush_mode {
	BEV_NORMAL = 0,
	BEV_FLUSH = 1,
	BEV_FINISHED = 2
}

    




extern(C):

alias void function(bufferevent*, void*) bufferevent_data_cb;
alias void function(bufferevent*, ev_short, void*) bufferevent_event_cb;
alias bufferevent_filter_result function( evbuffer *src, evbuffer *dst, ev_ssize_t dst_limit, bufferevent_flush_mode mode,void *ctx) bufferevent_filter_cb;

bufferevent* bufferevent_socket_new(event_base *base, evutil_socket_t fd, ev_int options);
ev_int bufferevent_socket_connect( bufferevent *,  sockaddr *, ev_int);
ev_int bufferevent_socket_connect_hostname( bufferevent *b,  evdns_base *, ev_int,  char*, ev_int);
ev_int bufferevent_socket_get_dns_error( bufferevent *bev);
ev_int bufferevent_base_set( event_base *base,  bufferevent *bufev);
ev_int bufferevent_priority_set( bufferevent *bufev, ev_int pri);
void bufferevent_free( bufferevent *bufev);
void bufferevent_setcb( bufferevent *bufev, bufferevent_data_cb readcb, bufferevent_data_cb writecb,  bufferevent_event_cb eventcb, void *cbarg);
ev_int bufferevent_setfd( bufferevent *bufev, evutil_socket_t fd);
evutil_socket_t bufferevent_getfd( bufferevent *bufev);
bufferevent *bufferevent_get_underlying( bufferevent *bufev);
ev_int bufferevent_write( bufferevent *bufev,  void *data, size_t size);
ev_int bufferevent_write_buffer( bufferevent *bufev,  evbuffer *buf);
size_t bufferevent_read( bufferevent *bufev, void *data, size_t size);
ev_int bufferevent_read_buffer( bufferevent *bufev,  evbuffer *buf);
evbuffer *bufferevent_get_input( bufferevent *bufev);
evbuffer *bufferevent_get_output( bufferevent *bufev);
ev_int bufferevent_enable( bufferevent *bufev, ev_short event);
ev_int bufferevent_disable( bufferevent *bufev, ev_short event);
ev_short bufferevent_get_enabled( bufferevent *bufev);
ev_int bufferevent_set_timeouts( bufferevent *bufev,   timeval *timeout_read,   timeval *timeout_write);
void bufferevent_setwatermark( bufferevent *bufev, ev_short events,size_t lowmark, size_t highmark);
ev_int bufferevent_flush( bufferevent *bufev, ev_short iotype, bufferevent_flush_mode mode);
bufferevent* bufferevent_filter_new( bufferevent *underlying, bufferevent_filter_cb input_filter, bufferevent_filter_cb output_filter,  ev_int options, void (*free_context)(void *),  void *ctx);
ev_int bufferevent_pair_new( event_base *base, ev_int options,  bufferevent *pair[2]);
ev_token_bucket_cfg *ev_token_bucket_cfg_new(ev_uint32_t read_rate, ev_uint32_t read_burst,ev_uint32_t write_rate, ev_uint32_t write_burst,timeval *tick_len);
void ev_token_bucket_cfg_free( ev_token_bucket_cfg *cfg);
ev_int bufferevent_set_rate_limit( bufferevent *bev, ev_token_bucket_cfg *cfg);
bufferevent_rate_limit_group *bufferevent_rate_limit_group_new( event_base *base,   ev_token_bucket_cfg *cfg);
ev_int bufferevent_rate_limit_group_set_cfg( bufferevent_rate_limit_group *,  ev_token_bucket_cfg *);
void bufferevent_rate_limit_group_free( bufferevent_rate_limit_group *);
ev_int bufferevent_add_to_rate_limit_group( bufferevent *bev, bufferevent_rate_limit_group *g);
ev_int bufferevent_remove_from_rate_limit_group( bufferevent *bev);
ev_ssize_t bufferevent_get_read_limit( bufferevent *bev);
ev_ssize_t bufferevent_get_write_limit( bufferevent *bev);
ev_ssize_t bufferevent_get_max_to_read( bufferevent *bev);
ev_ssize_t bufferevent_get_max_to_write( bufferevent *bev);
ev_ssize_t bufferevent_rate_limit_group_get_read_limit( bufferevent_rate_limit_group *);
ev_ssize_t bufferevent_rate_limit_group_get_write_limit( bufferevent_rate_limit_group *);
ev_int bufferevent_decrement_read_limit( bufferevent *bev, ev_ssize_t decr);
ev_int bufferevent_decrement_write_limit( bufferevent *bev, ev_ssize_t decr);
ev_int bufferevent_rate_limit_group_decrement_read( bufferevent_rate_limit_group *, ev_ssize_t);
ev_int bufferevent_rate_limit_group_decrement_write( bufferevent_rate_limit_group *, ev_ssize_t);
void bufferevent_rate_limit_group_get_totals( bufferevent_rate_limit_group *grp, ev_uint64_t *total_read_out, ev_uint64_t *total_written_out);
void bufferevent_rate_limit_group_reset_totals( bufferevent_rate_limit_group *grp);

/* compat */
alias bufferevent_data_cb evbuffercb;
alias bufferevent_event_cb everrorcb;
bufferevent* bufferevent_new(evutil_socket_t fd, evbuffercb readcb, evbuffercb writecb, everrorcb errorcb, void *cbarg);
void bufferevent_settimeout(bufferevent *bufev,ev_int timeout_read, ev_int timeout_write);
